﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using IESN.DAL;
using DevExpress.XtraEditors;
using IESN.IESNDataSetTableAdapters;
using IESN.BusinessCore;

namespace IESN
{
    /// <summary>
    /// 用户登录界面
    /// </summary>
    public partial class LoginFrm : XtraForm
    {
        #region 构造函数

        public LoginFrm()
        {
            InitializeComponent();
            
            ///*
            //* DevExpress的皮肤样式名称
            //* Metropolis,McSkin,Visual Studio 2013 Blue
            //*/
            //DevExpress.LookAndFeel.UserLookAndFeel.Default.SetSkinStyle("Visual Studio 2013 Blue");
            DevExpress.LookAndFeel.UserLookAndFeel.Default.SetSkinStyle("Black"); // 
        }

        #endregion
        
        #region 按钮事件

        /// <summary>
        /// 取消登录按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCannel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// 点击登录按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnLogin_Click(object sender, EventArgs e)
        {
            var aUserName = txtUserName.Text.Trim();
            var aPwd = txtPWD.Text.Trim();
            if (ValidateUser(aUserName, aPwd))
            {
                this.Visible = false;

                //初始化参数Params & Controller & DMObject collection
                if (Initialization.Init())
                {
                    MainIES mainIES = new MainIES(Initialization.MController);
                    mainIES.Show();
                }
                else
                {
                    MessageBox.Show("初始化失败");
                    this.Visible = true;
                }
            }
        }

        #region 用户合法性判断

        /// <summary>
        /// 用户合法性判断
        /// </summary>
        /// <param name="theUserName"></param>
        /// <param name="thePwd"></param>
        /// <returns></returns>
        private bool ValidateUser(string theUserName, string thePwd)
        {
            UserAccountTableAdapter userAccountTableAdapter = new UserAccountTableAdapter();
            IESNDataSet iESNDataSet = new IESNDataSet();
            userAccountTableAdapter.Fill(iESNDataSet.UserAccount);
            DataRow aLoginItem = null;
            foreach (DataRow aDr in iESNDataSet.UserAccount.Rows)
            {
                if (aDr["UserName"].ToString().ToLower() == theUserName.ToLower() && aDr["UserPwd"].ToString() == thePwd)
                {
                    aLoginItem = aDr;
                    break;
                }
            }
            if (aLoginItem == null)
            {
                return false;
            }

            if (aLoginItem["Status"].ToString().ToLower() == "false")
            {
                MessageBox.Show("该用户已禁用，请联系管理员!");
                return false;
            }

            return true;
        }

        #endregion

        #endregion
    }
}
